package com.tjhk.netty;

import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


public class NettyChannelManager {

    private static final Logger log = LoggerFactory.getLogger(NettyChannelManager.class);

    /**
     * KEY 默认为channel.id().asShortText()
     * 保存连接 Channel 的地方
     */
    private static Map<String, Channel> CHANNEL_POOL = new ConcurrentHashMap<>(60);
    private static Map<Channel, String> KEY_POOL = new ConcurrentHashMap<>(60);
    private static Map<String, String> CLIENT_IDS = new ConcurrentHashMap<>(60);

    /**
     * @param key
     * @param channel
     */
    public static void add(String key, Channel channel) {
        CHANNEL_POOL.put(key, channel);
        KEY_POOL.put(channel, key);
    }

    /**
     * 添加添加channel与客户端标志
     * @param channel
     * @param clientId
     */
    public static void addClientId(Channel channel, String clientId) {
        String channelId = KEY_POOL.get(channel);
        CLIENT_IDS.put(channelId, clientId);

    }

    /**
     * 删除 Channel
     * @param key
     */
    public static void remove(String key) {
        Channel channel = CHANNEL_POOL.get(key);
        if (channel == null) {
            return;
        }
        CHANNEL_POOL.remove(key);
        KEY_POOL.remove(channel);
    }

    /**
     * 删除并同步关闭连接
     * @param key
     */
    public static void removeAndClose(String key) {
        Channel channel = CHANNEL_POOL.get(key);
        remove(key);
        if (channel != null) {
            // 关闭连接
            try {
                channel.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 根据ClientId
     * 删除并同步关闭连接
     * @param clientId
     */
    public static void removeAndCloseByClientId(String clientId) {
        String key = CLIENT_IDS.get(clientId);
        removeAndClose(key);
    }
    /**
     * 删除并关闭连接
     * @param channel
     */
    public static void removeAndClose(Channel channel) {
        String key = KEY_POOL.get(channel);
        removeAndClose(key);
    }

    /**
     * 获得 Channel
     * @param key
     * @return String
     */
    public static Channel getChannel(String key) {
        return CHANNEL_POOL.get(key);
    }

    /**
     * 根据客户端ID获取channel
     * @param clientId
     * @return
     */
    public static Channel getChannelByClientId(String clientId) {
        String key = CLIENT_IDS.get(clientId);
        return CHANNEL_POOL.get(key);
    }

    /**
     * 获得 key
     * @param channel
     * @return Channel
     */
    public static String getKey(Channel channel) {
        return KEY_POOL.get(channel);
    }

    /**
     * 判断是否存在key
     * @param key
     * @return boolean
     **/
    public static boolean hasKey(String key) {
        return CHANNEL_POOL.containsKey(key);
    }

    /**
     * 判断是否存在channel
     * @param channel
     * @return boolean
     **/
    public static boolean hasChannel(Channel channel) {
        return KEY_POOL.containsKey(channel);
    }

}